package org.lc.kcjxzj.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.mozilla.universalchardet.UniversalDetector;

public class EncodingDetector {
    public static String detectEncoding(Path file) throws IOException {
        byte[] buffer = new byte[4096];
        try (var is = Files.newInputStream(file)) {
            UniversalDetector detector = new UniversalDetector(null);
            int nread;
            while ((nread = is.read(buffer)) > 0 && !detector.isDone()) {
                detector.handleData(buffer, 0, nread);
            }
            detector.dataEnd();
            return detector.getDetectedCharset();
        }
    }
}